package cn.fl.warrant.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.warrant.service.IArcBorrowFaceService;
import cn.fl.warrant.vo.ArcBorrowFaceVO;

import com.alibaba.fastjson.JSONObject;

import fl.warrant.facade.archive.condition.ArcBorrowFaceQueryCondition;
import fl.warrant.facade.archive.constant.ArchiveConstants;

/**
 * @Description: 面对面借阅 控制层
 */
@Controller
@RequestMapping("/archive/arcBorrowFace")
public class ArcBorrowFaceController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ArcBorrowFaceController.class);
	
	
	@Autowired
	private IArcBorrowFaceService arcBorrowFaceService;
	@Autowired
	private ICstBscInfoService cstBscInfoService;//客户
	
	/**
	 * 跳转到列表页面
	 */
	@RequestMapping("/toListPage.do")
	public String toListPage(HttpServletRequest request) {
		this.setAttribute(request);
		return "archive/arcBorrowFace/arcBorrowFaceList";
	}

	/**
	 * 列表页面查询
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/initList.do")
	@ResponseBody
	public Object initList(ArcBorrowFaceQueryCondition qc,HttpServletRequest request) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		
		page.setCondition(qc);
		Pager pager = this.arcBorrowFaceService.searchListPage(page);
		List<ArcBorrowFaceVO> list = (List<ArcBorrowFaceVO>) pager.getResultList();
		return new ListPageVO<ArcBorrowFaceVO>(list, pager.getTotalCount());
	}

	
	/**
	 * 跳转到保存页面(新增和修改)
	 */
	@RequestMapping(value = "/toSave.do")
	public Object toSave(HttpServletRequest request, HttpServletResponse response) {
		this.setAttribute(request);
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)) {
			request.setAttribute("entity", arcBorrowFaceService.selectById(Long.valueOf(id)));
		}
		return "archive/arcBorrowFace/arcBorrowFaceSave";
	}

	
	/**
	 * 新增和修改
	 */
	@RequestMapping(value = "/save.do")
	@ResponseBody
	public Object save(ArcBorrowFaceVO entity,HttpServletRequest request, HttpServletResponse response){
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		try {
			if(entity.getId() == null) {//新增
				entity.setIsValid(ArchiveConstants.IS_VALID);
				entity.setIsDel(ArchiveConstants.IS_DEL);
				entity.setCrtTm(new Date());
				entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());

				Long id = this.arcBorrowFaceService.add(entity);
				result.setData(id);
			}else {//修改
				ArcBorrowFaceVO arcBorrowFaceVO = arcBorrowFaceService.selectById(entity.getId());
				if(arcBorrowFaceVO != null) {
					arcBorrowFaceVO = entity;
					arcBorrowFaceVO.setMdfTm(new Date());
					arcBorrowFaceVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					
					this.arcBorrowFaceService.update(arcBorrowFaceVO);
					result.setInfo(SysLogConstant.LOG_CMM_UPDATE_SUCC);
				}
			}
			BaseLogUtils.info(logger, "save", "操作数据. DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "save", "操作数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "save", "操作数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(), ex);
		}
		return result;
	}
	
	/**
	 * 逻辑删除
	 */
	@RequestMapping(value = "/removeById.do")
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.arcBorrowFaceService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]", CurrentThreadContext.getCurrentUserName(), ex);
		}

		return result;
	}

	private void setAttribute(HttpServletRequest request) {
//		String sysCd = CurrentThreadContext.getCurrentSysCd(); 
//		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		//客户类型
		request.setAttribute("cstTypCdList",this.cstBscInfoService.searchCstTypCdList());
		//事业部
		request.setAttribute("buOrgCdList", arcBorrowFaceService.getKvDescList(ArchiveConstants.BU_ORG_CD));
		
	}
	
}

